package BackTrackingSeries;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

class PermuteSolution_46 {
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();

    public List<List<Integer>> permute(int[] nums) {
        backTracking(nums, path);
        return result;

    }

    public void backTracking(int[] nums, LinkedList<Integer> path) {

        // 回溯终止条件
        if (path.size() == nums.length) {
            result.add(new LinkedList<>(path));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            // 记录此时path里都有哪些元素使用了
            // 一个排列里一个元素只能使用一次
            if (path.contains(nums[i])) {
                continue;
            }
            path.add(nums[i]);
            backTracking(nums, path);
            path.removeLast();
        }
    }
}
